<script setup>
import { ref } from 'vue'
//重置密码的数据模型
const rePasswordModel = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
})
//校验第二次输入的新密码函数
const checkRePassWord = (rule,value,callback) => {
    if(value === ''){
        callback(new Error('请再输入新密码'))
    }else if(value !== rePasswordModel.value.new_pwd){
        callback(new Error('两次新密码不一致'))
    }else{
        callback()
    }

}

//定义表单校验规则
const rules = {
    old_pwd:[
        {required: true, message:'请输入原密码',trigger:'blur'},
        {min:5,max:16, message:'长度为5~16个字符',trigger:'blur'}
    ],
    new_pwd:[
        {required: true, message:'请输入新密码',trigger:'blur'},
        {min:5,max:16, message:'长度为5~16个字符',trigger:'blur'}
    ],
    re_pwd:[
        {required: true,validator:checkRePassWord,trigger:'blur'}
    ]
}


//更新用户密码函数 
import { ElMessage } from 'element-plus'
import { userUpdatePasswordService } from '@/api/user.js';
import { useUserInfoStore } from '@/stores/userInfo';
import { useTokenStore } from '@/stores/token';
import router from '@/router';
const userInfoStore = useUserInfoStore();
const tokenStore = useTokenStore();
const updatePassword = async() => {
    let result = await userUpdatePasswordService(rePasswordModel.value);
    ElMessage.success(result.message ? result.message : "更改成功！")
    ElMessage.error('密码更改，请重新登录！')
    userInfoStore.removeInfo();
    tokenStore.removeToken();

    router.push('/login')
}

</script>



<!-- 表单项中 prop 属性对应 <el-form> 标签中定义的 rules 对象中的验证规则-->
    <!-- type 为password时可以删除autocomplete -->
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="rePasswordModel" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="old_pwd">
                        <el-input v-model="rePasswordModel.old_pwd" type="password" placeholder="请输入原密码"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd" >
                        <el-input v-model="rePasswordModel.new_pwd" placeholder="请输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item label="重复密码" prop="re_pwd" >
                        <el-input v-model="rePasswordModel.re_pwd" type="password" placeholder="请再次输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePassword">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>
